package cn.tedu.api;

import java.util.Objects;

/*本类用于测试顶级父类object*/
public class TestObject {
    public static void main(String[] args) {
        Student s=new Student("gebolin",100);
        Student s2=new Student("gebolin",100);
        System.out.println(s);
        System.out.println(s.name);
        System.out.println(s.hashCode());
        System.out.println(s2.hashCode());
        /*equals()的默认实现时==比较，比较的是两个对象的地址值
        * 如果不想比较地址，想比较类型和属性值，需要重写此方法*/
        System.out.println(s.equals(s2));

    }
}
class Student{

    //定义属性
    String name;
    int age;

    public Student() {
    }

    public Student(String nam, int age) {

        this.name = nam;
        this.age = age;

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
